package com.lanou3g.ptp.user.service.impl;

import com.lanou3g.ptp.entity.User;
import com.alibaba.dubbo.config.annotation.Service;
import com.lanou3g.ptp.user.mapper.UserMapper;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.ptp.user.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UserMapper userMapper;

    @Override
    public User userById(Long id) {
        User fin = userMapper.userById(id);
        return fin;
    }

    //模糊查询
    @Override
    public List<User> userByName(String name) {
        List<User> users = userMapper.userByName("%" + name + "%");
        return users;
    }

    //通过id删除
    @Override
    public int deleteById(Long id) {
        int i = userMapper.deleteById(id);
        return i;
    }

    //更改用户
    @Override
    public int upUser(User user) throws Exception {
        String password = user.getTel().substring(5);
        String password1 = MD5Utils.md5(password, "654321");
        user.setPassword(password1);
        userMapper.upUser(user);
        return 0;
    }

    //添加用户
    @Override
    public Long addUser(User user) throws Exception {
        // 加盐
        String password = user.getTel().substring(5);
        String password1 = MD5Utils.md5(password, "123456");
        // 把加盐后的密码 付给user
        user.setPassword(password1);
        // 把user添加进库
        userMapper.addUser(user);

        return userMapper.userByNo(user.getNo());
    }


}
